
package ${basePackage}.${subPackage}.service;


import ${basePackage}.${subPackage}.dao.${upperFirst(name)}Dao;
import ${basePackage}.${subPackage}.entity.${upperFirst(name)};

<?py if extend != "":?>
//此处导入本类的父类

<?py  properties = properties + models[extend]["properties"] ?>

<?py  if extend not in baseDataTypes: ?>

<?py    #print extend ?>

import ${basePackage}.${models[extend]["subPackage"]}.entity.${upperFirst(extend)};

<?py  #endif?>


<?py #endif?>

//此处导入本类引用的类
<?py for property in properties: ?>
<?py  if property["dataType"] not in baseDataTypes: ?>
<?py    datatype =  property["dataType"]?>
<?py    #print datatype ?>

import ${basePackage}.${models[datatype]["subPackage"]}.entity.${upperFirst(datatype)};

<?py  elif property["dataType"] == "List":?>
<?py    elementType =  property["elementType"]?>
import ${basePackage}.${models[elementType]["subPackage"]}.entity.${upperFirst(elementType)};


<?py  #endif?>
<?py #endfor ?>

import com.bonc.common.bean.*;

import java.util.*;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
//import org.springframework.data.domain.Sort.Order;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 *
 * @author song
 */
@Service
@Transactional
public class ${upperFirst(name)}ServiceImpl implements ${upperFirst(name)}Service{
    
    
    @Autowired
    private ${upperFirst(name)}Dao ${name}Dao;

    @Override
    public ${upperFirst(name)} read(Long id) {
        if(id!=null)
         return get${upperFirst(name)}Dao().findById(id).get();
        else 
            return  null;
     
    }

    @Override
    public void update(${upperFirst(name)} ${name}) {
        
               get${upperFirst(name)}Dao().saveAndFlush(${name});
    }

    @Override
    public void delete(Long id) {
        
           get${upperFirst(name)}Dao().deleteById(id);
    }

    @Override
    public List listAll() {
       
       return    get${upperFirst(name)}Dao().findAll();
    }

    /**
     * @return the ${name}Dao
     */
    public ${upperFirst(name)}Dao get${upperFirst(name)}Dao() {
        return ${name}Dao;
    }

    /**
     * @param ${name}Dao the ${name}Dao to set
     */
    public void set${upperFirst(name)}Dao(${upperFirst(name)}Dao ${name}Dao) {
        this.${name}Dao = ${name}Dao;
    }

    public ${upperFirst(name)} create(${upperFirst(name)} ${name}) {
        
 
          
    
         return   get${upperFirst(name)}Dao().save(${name});
    
    }

    
     @Override
    public Map  findByCondition(final ${upperFirst(name)} ${name},PageContent page){
    
             
        
         Specification querySpecifi = new Specification<${upperFirst(name)}>() {
            @Override
            public Predicate toPredicate(Root<${upperFirst(name)}> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
              
                List<Predicate> predicates = new ArrayList();
                if(${name}.getId()!=null){
                    
                          predicates.add(cb.equal(root.get("id"), ${name}.getId()));
                 }

    <?py if extend != "":?>
    <?py    properties=models[extend]["properties"] + properties?>
   
    <?py #endif?>
    <?py for property in properties: ?>
    
    
    <?py    if property["dataType"] == "String":?>              
                
                 if(${name}.get${upperFirst(property["name"])}()!=null && !${name}.get${upperFirst(property["name"])}().equals("")){

                          predicates.add(cb.like(root.get("${property["name"]}").as(${property["dataType"]}.class), "%"+${name}.get${upperFirst(property["name"])}()+"%"));
                 }
    <?py    elif property["dataType"] == "List":?>

                 if(${name}.get${upperFirst(property["name"])}()!=null && ${name}.get${upperFirst(property["name"])}().toArray().length>0){
                            Join<${upperFirst(name)}, ${upperFirst(property["elementType"])}> ${property["elementType"]}Join = root.join("${property["name"]}", JoinType.LEFT);
                             predicates.add(cb.equal(${property["elementType"]}Join.get("id"), ((${upperFirst(property["elementType"])})${name}.get${upperFirst(property["name"])}().toArray()[0]).getId()));
                 }
    <?py    else:?>
                 if(${name}.get${upperFirst(property["name"])}()!=null ){
                             predicates.add(cb.equal(root.get("${property["name"]}"), ${name}.get${upperFirst(property["name"])}()));
                 }
    <?py    #endif  ?>
                    
                 
    <?py  #endfor ?>               
                    
                  
                
               
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
            
            
           
         
        };
         
         Sort sort = null;  
         
         String order =page.getOrder();
         String page1 = page.getPage();
         String sort1 = page.getSort();
         String rows1 = page.getRows();
         
         //对空值作处理
         if(rows1 ==null || rows1.equals("")){
             rows1 ="10";
         }
         if(order==null||order.equals("")){  
             order = "asc";
         }
         if(page1==null || page1.equals("")){
             page1 = "1";
         }
         
         if(sort1 == null || sort1.equals("")){
             sort1= "id";
         }
        if(order.equals("asc")){  
	     //Order order1 = Sort.Order.asc(sort1);
	     //List list1 = new ArrayList();
	     //list1.add(sort1);
             sort = Sort.by(Direction.ASC,sort1);  
        }else if(order.equals("desc")){  
	     // Order order1 = Sort.Order.desc(sort1);
	     // List list1 = new ArrayList();
	    //  list1.add(sort1);
             sort = Sort.by(Direction.DESC,sort1);  
        }  
        int pageNum = Integer.parseInt(page1)-1;  
        int rows = Integer.parseInt(rows1);  
        //Pageable pageable = new PageRequest(pageNum,rows,sort);  
          Pageable pageable = PageRequest.of(pageNum,rows,sort);
         Page<${upperFirst(name)}> list =  ${name}Dao.findAll(querySpecifi,pageable);
         
             Map map = new HashMap();  
        map.put("total", list.getTotalElements());//数据总数  
        map.put("rows",list.getContent());//分页应该显示的数据  
      
        return map;
    
    
    }

    

    
}
